package com.geqian.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geqian.common.common.PageRequest;
import com.geqian.common.common.PageResult;
import com.geqian.common.common.ResponseResult;
import com.geqian.web.entity.ApiLog;
import com.geqian.web.mapper.ApiLogMapper;
import com.geqian.web.service.ApiLogService;
import com.geqian.web.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 接口访问日志服务实现
 *
 * @author geqian
 * @date 2023-12-10 18:34:11
 */
@Service
public class ApiLogServiceImpl extends ServiceImpl<ApiLogMapper, ApiLog> implements ApiLogService {

    @Override
    public ResponseResult<PageResult<ApiLog>> getApiLogList(PageRequest pageRequest) {
        LambdaQueryWrapper<ApiLog> wrapper = new LambdaQueryWrapper<>();
        String key = pageRequest.getKey();
        if (StringUtils.hasText(key)) {
            wrapper.like(ApiLog::getIp, key).or().like(ApiLog::getUserName,key);
        }
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<ApiLog> apiLogList = this.baseMapper.selectList(wrapper);
        PageInfo<ApiLog> pageInfo = new PageInfo<>(apiLogList);
        PageResult<ApiLog> pageResult = PageUtils.getPageResult(pageInfo);
        return ResponseResult.success(pageResult);
    }


    @Override
    public ResponseResult<String> deleteApiLog(List<ApiLog> apiLogList) {
        if (apiLogList != null && !apiLogList.isEmpty()) {
            try {
                this.baseMapper.deleteBatch(apiLogList);
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseResult.fail("删除失败");
            }
        }
        return ResponseResult.success();
    }

}
